const tool = require('../tools/tool')

module.exports = {
    parseAnswerDoc: {
        exp: '解析答案文档内容',
        method: 'post',
        auth: {
            admin: true
        },
        async func(data) {
            let re = await tool.readDocumentText(`./files/${data.param.fileName}`)
            if (typeof re === 'string') {
                re = re.replaceAll('\n', '')
                re = re.replaceAll(' ', '')
                re = re.replaceAll('\t', ' ')
                re = tool.parseNumberedSections(re)

                for (let i = 0; i < Object.keys(re).length; i++) {
                    let key = Object.keys(re)[i]
                    let value = re[key]
                    re[key] = {
                        answer: (() => {
                            let str = value.split('解析')[0]
                            str = str.replaceAll('【', '')
                            str = str.replaceAll('答案', '')
                            str = str.replaceAll('】', '')
                            str = str.trimEnd()
                            str = str.trimStart()

                            if (str.indexOf('、') + 1) {
                                str = str.split('、')
                                return str
                            }
                            let t = str.split('')
                            for (let i = 0; i < t.length; i++) {
                                t[i] = t[i].toUpperCase()
                            }
                            return t
                        })(),
                        analysis: (() => {
                            let str = value.split('解析')[1]
                            str = str.replaceAll('【', '')
                            str = str.replaceAll('】', '')
                            str = str.trimEnd()
                            str = str.trimStart()

                            return str
                        })()
                    }
                }
            }
            setTimeout(() => {
                tool.deleteFile(data.param.fileName)
            }, 1000);
            return re
        }
    },
    parseQuestionDoc: {
        exp: '解析问题文档内容',
        method: 'post',
        auth: {
            admin: true
        },
        async func(data) {
            // let test = await tool.extractDocStream(`./files/${data.param.fileName}`)
            // console.log(test.filter(item => item.type === 'text').length)
            // console.log(test.filter(item => item.type === 'image').length)
            // tool.saveBufferImage(await tool.createLabeledImage(tool.selectValueFromArray(test.filter(item => item.type === 'image'), 'value')))
            let image = await tool.extractImagesFromFile(`./files/${data.param.fileName}`)
            let re = await tool.readDocumentText(`./files/${data.param.fileName}`)
            if (typeof re === 'string') {
                re = re.replaceAll('\n', '')
                re = re.replaceAll(' ', '')
                re = re.replaceAll('\t', ' ')
                // console.log(re)
                let options = [{ c: 'A.', num: re.split('A.').length - 1 }, { c: 'A、', num: re.split('A、').length - 1 },].sort((a, b) => {
                    return b.num - a.num
                })
                let Anum = re.split('A').length - 1
                const different = (a, b) => {
                    if (a > b) {
                        if (a - b < 10) return false
                        return true
                    }
                    if (b - a < 10) return false
                    return true
                }
                if (!different(options[0].num, options[1].num)) {
                    if (different(Anum, options[0].num) && different(Anum, options[1].num)) {
                        re = re.replaceAll('A', 'A、')
                        re = re.replaceAll('B', 'B、')
                        re = re.replaceAll('C', 'C、')
                        re = re.replaceAll('D', 'D、')
                    }
                }


                re = tool.parseNumberedSections(re)

                for (let i = 0; i < Object.keys(re).length; i++) {
                    let key = Object.keys(re)[i]
                    let value = re[key]
                    re[key] = tool.parseSingleQuestion(value, key)
                }
            }
            let arr = []
            for (let i = 0; i < Object.keys(re).length; i++) {
                let key = Object.keys(re)[i]
                let value = re[key]
                arr.push({
                    index: parseInt(key),
                    ...value
                })
            }
            setTimeout(() => {
                tool.deleteFile(data.param.fileName)
            }, 1000);
            return {
                parsed: arr,
                image,
                report: await (async () => {
                    let re1 = {}
                    for (let i = 0; i < Object.keys(re).length; i++) {
                        let key = Object.keys(re)[i]
                        let value = re[key]
                        if (Object.keys(value.options).length !== 4) {
                            let reason = '选项数量不足4个'
                            if (re1[reason] === undefined) {
                                re1[reason] = []
                            }
                            re1[reason].push(key)
                        }
                    }
                    return re1
                })()
            }
        }
    }
}